﻿using System;
using System.IO;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using System.Text;

namespace insa.rtos
{
    /// <summary>
    /// Declaration of the package providing the Simulation code generator
    /// </summary>
    [ProvideCodeGenerator(typeof(CodeGenerator), "RTOSCodeGenerator",
     "Generates C implementation of RTOS described in .rtos files", true)]
    internal sealed partial class RTOSPackage
    {

    }


    [System.Runtime.InteropServices.Guid("3925C692-C664-44E2-8396-D06E5EA924CC")]
    class CodeGenerator : TemplatedCodeGenerator
    {
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent) 
        {

            // Replace the supplied file contents with the template we want to run 
            inputFileContent = ASCIIEncoding.UTF8.GetString(insa.rtos.dsl.CustomCode.CodeGeneration.CodeGenerationResource.RTOSDsl);
            
            // Substitute the name of the current model file into the template. 
            FileInfo fi = new FileInfo(inputFileName);
            inputFileContent = inputFileContent.Replace("Test.rtos", fi.Name);
            //inputFileContent = inputFileContent.Replace("namespace ShapesTest", "namespace " + FileNamespace);
            // Now just delegate the rest of the work to the base class 
            byte[] data; 
            data = base.GenerateCode(inputFileName, inputFileContent);
            byte[] ascii = new byte[data.Length - 3];
            Array.Copy(data, 3, ascii, 0, data.Length - 3);
            return ascii;
        }
    }
}
